package com.bs.sc.base.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DataUtil {

	/**
	 * 获取推荐数据模型
	 * 
	 * @param list
	 * @return
	 */
	public static Map<String, Map<String, String>> getRecommendData(List<String> list) {
		Map<String, Map<String, String>> map = new HashMap<>();
		for (String s : list) {
			Map<String, String> temp = new HashMap<String, String>();
			String[] ss = s.split(",");
			String userId = ss[0];
			String goodsId = ss[1];
			String rating = ss[2];
			temp.put(goodsId, rating);
			if (map.containsKey(userId)) {
				Map<String, String> t = map.get(userId);
				t.put(goodsId, rating);
				map.put(userId, t);
			} else {
				map.put(userId, temp);
			}
		}
		return map;
	}

	/**
	 * 对Map的值进行降序排列
	 * 
	 * @param map
	 * @return
	 */
	public static <K, V extends Comparable<? super V>> Map<K, V> sortDescend(Map<K, V> map) {
		List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			@Override
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				int compare = (o1.getValue()).compareTo(o2.getValue());
				return -compare;
			}
		});
		Map<K, V> returnMap = new LinkedHashMap<K, V>();
		for (Map.Entry<K, V> entry : list) {
			returnMap.put(entry.getKey(), entry.getValue());
		}
		return returnMap;
	}
}
